package itesm.coler.data;

import itesm.coler.data.DAOUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.StringTokenizer;

import javax.sql.*;

import itesm.coler.models.MessageType;


public class MySQLMessageTypeDAO implements MessageTypeDAO {
Connection connection = null;
	
	public MySQLMessageTypeDAO(){
		
		// MySQLUserDAO implementation of the UserDAO interface. 
		// This class can contain all MySQL specific code and SQL statements. 
		// 
		// The client is thus shielded from knowing 
		// these implementation details.
	}
	
	 public Connection getConnection(){
			if (connection == null)
			    connection = MySQLDAOFactory.createConnection();
			return connection;
		}
		
		public void setConnection(Connection connection) {
			this.connection = connection;
		}

		@Override
		public MessageType findMessageTypeByMessageTypeId(String messagetypeid)
				throws DAOException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public MessageType findMessageTypeByMessageTypeName(
				String messagetypename) throws DAOException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public ArrayList<MessageType> findAll() throws DAOException {
			// TODO Auto-generated method stub
			PreparedStatement findMessageType = null;
	         ResultSet results= null;
			 ArrayList<MessageType> messagetypeList = null;
			 try {
			       connection = getConnection();
			       findMessageType = connection.prepareStatement("SELECT * FROM messagetype");
			       synchronized(findMessageType) {
			       // obtain list of users	  
			    	  results = findMessageType.executeQuery();
			       // get rows data{
			    	  messagetypeList = new ArrayList<MessageType>();
			         while (results.next()) {
			        	 MessageType messagetype = mapResultSetToObject(results);		
			        	 messagetypeList.add(messagetype);	   
			         }
			       }
			   } catch (SQLException e){
				   throw new DAOException(e);
			   } finally {
					DAOUtil.close(connection,findMessageType,results);
			   }
			  return messagetypeList;
			
		}

		@Override
		public ArrayList<MessageType> findAllByCategory(String messagecategoryid)
				throws DAOException {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public int insertUser(MessageType messagetype)
				throws IllegalArgumentException, DAOException {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public RowSet selectUsersRS() {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public Collection<MessageType> selectMessageTypeTO() {
			// TODO Auto-generated method stub
			return null;
		}
		
		
		public MessageType mapResultSetToObject(ResultSet results) throws SQLException {
			  System.out.println("Voy a mapear el objeto user");
			  MessageType messagetype = new MessageType();

			  String messageCategoryId = results.getString("messagecategoryid");
			  if (messageCategoryId != null) {
				  DAOFactory MySQLFactory = DAOFactory.getDAOFactory(DAOFactory.MySQL);
			  	     MessageCategoryDAO messageCategoryDAO = MySQLFactory.getMessageCategoryDAO();
		             messagetype.setMessageCategory(messageCategoryDAO.findMessageCategoryByMessageCategoryId(messageCategoryId));
			  }
			 messagetype.setMessagetypeid(results.getString("messagetypeid"));
			 messagetype.setMessagetypename(results.getString("messagetypename"));

			 
			 // Integer teamId = results.getInt("teamid");
			 // System.out.println("teamId = "+teamId);
			 // user.setTeamId(teamId.intValue());
	/*		  if ((teamId != null)&& (teamId !=0)) {
			         DAOFactory MySQLFactory = DAOFactory.getDAOFactory(DAOFactory.MySQL);
			  	     TeamDAO teamDAO = MySQLFactory.getTeamDAO(connection);
		             user.setTeam(teamDAO.findTeamById(teamId.intValue()));
			  } */
			  return messagetype;
		 }

	
}
